package com.shfl.task.common;


import cn.hutool.json.JSONUtil;
import com.shfl.task.pagehelper.Pageable;
import com.shfl.task.pagehelper.PageableUtil;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class RequestHelper implements Serializable {

    private String search;

    private Integer page;

    private Integer pageSize;

    private Map<String, Object> keyMap;

    private Pageable pageable;

    public void apply() {
        if (this.page == null || this.page < 1) this.page = 1;
        if (this.pageSize == null || this.pageSize < 1) this.pageSize = SystemConstant.DEFAULT_PAGE_SIZE;
        this.keyMap = new HashMap<>();

        if (StringUtils.isNotEmpty(this.search)) {
            String res = StringEscapeUtils.unescapeJava(this.search);
            this.keyMap = JSONUtil.toBean(res, Map.class);
        }
        this.pageable = PageableUtil.getPageable(this.page, this.pageSize);
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Map<String, Object> getKeyMap() {
        return keyMap;
    }

    public void setKeyMap(Map keyMap) {
        this.keyMap = keyMap;
    }

    public Pageable getPageable() {
        return pageable;
    }

    public void setPageable(Pageable pageable) {
        this.pageable = pageable;
    }
}
